package com.xuequ.answerme.adapter;

import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.xuequ.answerme.R;
import com.xuequ.answerme.beans.AnswerBean;

import java.util.List;

/**
 * Created by Shijie on 2015/9/29.
 */
public class DetailRecyclerViewAdapter extends RecyclerView.Adapter<DetailRecyclerViewAdapter.ViewHolder>{

    private List<AnswerBean> mDataList;//数据

    private OnItemClickListener mOnItemClickListener;
    private OnRefreshDoneListener mOnRefreshDoneListener;

    /**
     * 构造方法
     * @param mDataList
     */
    public DetailRecyclerViewAdapter(List<AnswerBean> mDataList) {
        this.mDataList = mDataList;
    }

    /**
     * Item点击接口
     */
    public interface OnItemClickListener{
        void onItemClick(View view, int position);// 点击答案时触发
    }

    public interface OnRefreshDoneListener{
        void onRefreshDone();
        void onLoadMore();
        void onLoadMoreDone();
    }

    public void setOnRefreshDoneListener(OnRefreshDoneListener mOnRefreshDoneListener){
        this.mOnRefreshDoneListener = mOnRefreshDoneListener;
    }

    /**
     * 接口绑定
     * @param mOnItemClickListener
     */
    public void setOnItemClickListener(OnItemClickListener mOnItemClickListener){
        this.mOnItemClickListener = mOnItemClickListener;
    }

    /**
     * 返回
     * @param viewGroup
     * @param viewType
     * @return
     */
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.question_detail_recycler_item,viewGroup,false);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        Log.i("Adapter", "position------------> " +position);
        holder.mUsername.setText(mDataList.get(position).getAnswerer().getUsername());
        Log.i("Adapter", "title------------> " + mDataList.get(position).getAnswerer().getObjectId());
        holder.mContent.setText(mDataList.get(position).getAnswer_content());
        Log.i("Adapter", "answer------------> " + mDataList.get(position).getAnswer_content());
        Log.i("Adapter", "-----------------------------------------------");

        /**
         * 设置绑定点击事件
         */
        if(mOnItemClickListener != null){
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnItemClickListener.onItemClick(holder.itemView, holder.getLayoutPosition());
                }
            });
        }
        if (mOnRefreshDoneListener != null && position == mDataList.size() - 1) {
            mOnRefreshDoneListener.onLoadMore();
        }
    }

    /**
     * 返回Item数
     * @return
     */
    @Override
    public int getItemCount() {
        return mDataList.size();
    }

    /**
     * 建立ViewHolder
     */
    public class ViewHolder extends RecyclerView.ViewHolder{

        public TextView mUsername;
        public TextView mContent;

        public ViewHolder(View itemView) {
            super(itemView);
            mUsername = (TextView) itemView.findViewById(R.id.detail_recycler_item_answer_username);
            mContent = (TextView) itemView.findViewById(R.id.detail_recycler_item_answer_content);
            //背景框
        }
    }

    public void refreshData(List<AnswerBean> mData){
        mDataList.clear();
        for(int i = 0; i < mData.size(); i++){
            mDataList.add(i,mData.get(i));
        }
        notifyDataSetChanged();
        if(mOnRefreshDoneListener != null){
            mOnRefreshDoneListener.onRefreshDone();
        }
    }

    public void addOldData(List<AnswerBean> answerBeans) {
        for (int i = 0; i < answerBeans.size(); i++) {
            mDataList.add(answerBeans.get(i));
            notifyItemInserted(mDataList.size() - 1);
        }
        if (mOnRefreshDoneListener != null) {
            mOnRefreshDoneListener.onLoadMoreDone();
        }
    }

}
